*************************************************************
* Replication codes for analyses in Section 5.2 of
* Beyond Yield Response: Weather Shocks and Crop Abandonment
* May 2020
*************************************************************

	use dta\dta_for_insure, clear

* estimation	
	xtset fips year
	gen lpl_acre_corn = log(pl_acre_corn)	
	gen lpl_acre_soy = log(pl_acre_soy)
	gen L_price_dfl_corn0 = L.price_dfl_corn0
	gen L_price_dfl_soy0 = L.price_dfl_soy0
	
	forvalues t = 10/33 {
		gen yeart`t' = (t==`t')
		}
		
	egen pl_yrs_corn89 = total(yield_corn!=.) if year>=1989, by(fips)
	egen pl_yrs_soy89 = total(yield_soy!=.) if year>=1989, by(fips)
	
	gen gdd8_26 = (dday8C - dday26C)/100
	gen gdd26_35 = dday26C - dday35C
	
* 1st stage
	reghdfe fraction_insured_corn L_price_dfl_corn0 lpl_acre_corn corn_subrate year ///
			if acres_grain_ratio_harvest!=. & irr_ratio < .1 & drop_corn==0 & pl_yrs_corn89>5, ///
			absorb(fips) vce(cluster fips stateyear)
	
	reghdfe fraction_insured_soy L_price_dfl_soy0 lpl_acre_soy soy_subrate year ///
			if acres_soy_ratio_harvest!=. & irr_ratio < .1 & drop_soy==0 & pl_yrs_soy89>5, ///
			absorb(fips) vce(cluster fips stateyear)	
	
* bootstrap program - corn
	program iv_insure_corn
		
		qui reghdfe fraction_insured_corn L_price_dfl_corn0 lpl_acre_corn corn_subrate year ///
				if acres_grain_ratio_harvest!=. & irr_ratio < .1 & drop_corn==0 & pl_yrs_corn89>5, ///
				absorb(fips) vce(cluster fips stateyear)
		qui predict iv_fraction_insured_corn		

		qui egen fc_yr = median(iv_fraction_insured_corn) if fraction_insured_corn!=., by(stateyear)
		qui gen fc = iv_fraction_insured_corn - fc_yr

		foreach x in prec prec2 gdd8_26 gdd26_35 dday35C {
			qui gen `x'Xfc = `x'*fc
			} 
		
		qui reghdfe acres_grain_ratio_harvest prec prec2 gdd8_26 gdd26_35 dday35C precXfc prec2Xfc gdd8_26Xfc gdd26_35Xfc dday35CXfc ///
			i.stateansi#c.t i.stateansi#c.t2 yeart* if irr_ratio < .1 & drop_corn==0 & pl_yrs_corn>5, ///
			absorb(fips) vce(cluster fips stateyear)		

		drop fc* iv_fr* *Xfc	
		
	end
	bootstrap, reps(1000): iv_insure_corn		

	estimates store ivinsure_boot_corn
	esttab ivinsure_boot_corn, b(4) se(5) keep(*dd* *prec*) order(gdd8* gdd26* dday* prec precX* prec2*)
	
	* effects evaluated at knots for plotting splines (Appendix Figure A11)

		lincom (gdd8_26 + 0.1*gdd8_26Xfc)/100*(26-8)
		lincom (gdd8_26 + 0.1*gdd8_26Xfc)/100*(26-8) + (gdd26_35 + 0.1*gdd26_35Xfc)*(35-26)
		lincom (gdd8_26 + 0.1*gdd8_26Xfc)/100*(26-8) + (gdd26_35 + 0.1*gdd26_35Xfc)*(35-26) + (dday35C + 0.1*dday35CXfc)*(41-35)

		lincom (gdd8_26 - 0.1*gdd8_26Xfc)/100*(26-8)
		lincom (gdd8_26 - 0.1*gdd8_26Xfc)/100*(26-8) + (gdd26_35 - 0.1*gdd26_35Xfc)*(35-26)
		lincom (gdd8_26 - 0.1*gdd8_26Xfc)/100*(26-8) + (gdd26_35 - 0.1*gdd26_35Xfc)*(35-26) + (dday35C - 0.1*dday35CXfc)*(41-35)


		
* bootstrap program - soy
	program iv_insure_soy
		
		qui reghdfe fraction_insured_soy L_price_dfl_soy0 lpl_acre_soy soy_subrate year ///
				if acres_soy_ratio_harvest!=. & irr_ratio < .1 & drop_soy==0 & pl_yrs_soy89>5, ///
				absorb(fips) vce(cluster fips stateyear)
		qui predict iv_fraction_insured_soy		

		qui egen fs_yr = median(iv_fraction_insured_soy) if fraction_insured_soy!=., by(stateyear)
		qui gen fs = iv_fraction_insured_soy - fs_yr

		foreach x in prec prec2 gdd8_26 gdd26_35 dday35C {
			qui gen `x'Xfs = `x'*fs
			} 
		
		reghdfe acres_soy_ratio_harvest prec prec2 gdd8_26 gdd26_35 dday35C precXfs prec2Xfs gdd8_26Xfs gdd26_35Xfs dday35CXfs ///
			i.stateansi#c.t i.stateansi#c.t2 yeart* if irr_ratio < .1 & drop_soy==0 & pl_yrs_soy>5, ///
			absorb(fips) vce(cluster fips stateyear)		

		drop fs* iv_fr* *Xfs	
		
	end
	bootstrap, reps(1000): iv_insure_soy		

	estimates store ivinsure_boot_soy
	esttab ivinsure_boot_soy, b(4) se(5) keep(*dd* *prec*) order(gdd8* gdd26* dday* prec precX* prec2*)

	* effects evaluated at knots for plotting splines (Appendix Figure A11)

		lincom (gdd8_26 + 0.1*gdd8_26Xfs)/100*(26-8)
		lincom (gdd8_26 + 0.1*gdd8_26Xfs)/100*(26-8) + (gdd26_35 + 0.1*gdd26_35Xfs)*(35-26)
		lincom (gdd8_26 + 0.1*gdd8_26Xfs)/100*(26-8) + (gdd26_35 + 0.1*gdd26_35Xfs)*(35-26) + (dday35C + 0.1*dday35CXfs)*(41-35)

		lincom (gdd8_26 - 0.1*gdd8_26Xfs)/100*(26-8)
		lincom (gdd8_26 - 0.1*gdd8_26Xfs)/100*(26-8) + (gdd26_35 - 0.1*gdd26_35Xfs)*(35-26)
		lincom (gdd8_26 - 0.1*gdd8_26Xfs)/100*(26-8) + (gdd26_35 - 0.1*gdd26_35Xfs)*(35-26) + (dday35C - 0.1*dday35CXfs)*(41-35)
